package test01.controller;


import com.csn.hello.service.HelloService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import test01.Result.Result;
import test01.model.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static test01.RabbitMQ.TopicRabbitConfig.man;
import static test01.RabbitMQ.TopicRabbitConfig.woman;

@Slf4j
@Controller
public class StudyTest {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    AmqpTemplate amqpTemplate;


    @GetMapping("/get")
    @ResponseBody
    public User getuser(User user){
        return user;
    }

    @GetMapping("/login")
    public String index(HttpServletRequest httpServletRequest){
        System.out.println("执行login逻辑");
        HttpSession session = httpServletRequest.getSession();
        session.setAttribute("login","1");
        return "index.html";
    }

    @GetMapping("/main")
    public String main(String a,String b){
        System.out.println("执行main逻辑");
        return "index.html";
    }

    @GetMapping("/main2")
    @ResponseBody
    public Result main2(){
        System.out.println("执行main逻辑");
        return  Result.success(null);
    }
    @GetMapping("/uploadImg")
    public String uploadImg(){
        System.out.println("uploadImg");
        return "uploadImg.html";
    }
    @PostMapping("/file_upload")
    public String uploadImg(@RequestPart("photoForm") MultipartFile photo, @RequestPart("photoForms") MultipartFile photos){
        System.out.println("uploadImg");
        log.info("photo:{}",photo);
        log.info("photos:{}",photos);
        return "uploadImg.html";
    }

    @GetMapping("/redirect")
    public String redirect(){
        return "redirect:index";
    }
    @PostMapping ("/Exception")
    @ResponseBody
    public Result Exception(){
        int b=10/0;
        return Result.success("success");
    }

    @GetMapping("sql")
    @ResponseBody
    public Result sql(){
        Long count=jdbcTemplate.queryForObject("select count(1) from goods",Long.class);
        System.out.println(count);
        return Result.success(null);
    }

    @Value("${person.name:}")
    private String name;

    @Autowired
    String red;
    @GetMapping("/t")
    @ResponseBody
    public String hello(){

        return "hello:"+name+"red:"+red;
    }

    @Autowired
    HelloService helloService;

    @GetMapping("/helloService")
    @ResponseBody
    public String helloService(){
        return helloService.sayHello("csn");
    }
    @GetMapping("/sendDirectReceiver")
    @ResponseBody
    public Result DirectReceiver(){
        Map testMessage=new HashMap<>();
        testMessage.put("messageId","123");
        amqpTemplate.convertAndSend("TestDirectExchange","TestDirectRouting",testMessage);

        return Result.success(null);
    }
    @GetMapping("/sendTopicMessage")
    @ResponseBody
    public Result TopicMessage(){
        Map testMessage=new HashMap<>();
        testMessage.put("123","man");
        amqpTemplate.convertAndSend("topicExchange",man,testMessage);

        return Result.success(null);
    }

    @GetMapping("/sendTopicMessage2")
    @ResponseBody
    public Result TopicMessage2(){
        Map testMessage=new HashMap<>();
        testMessage.put("123","man");
        amqpTemplate.convertAndSend("topicExchange",woman,testMessage);
        return Result.success(null);
    }

    @GetMapping("/sendFanoutRabbit")
    @ResponseBody
    public Result FanoutRabbit(){
        Map testMessage=new HashMap<>();
        testMessage.put("123","man");
        amqpTemplate.convertAndSend("fanoutExchange",null,testMessage);
        return Result.success(null);
    }

    //消息推送到server，但是在server里找不到交换机
    @GetMapping("/TestMessageAck")
    @ResponseBody
    public Result TestMessageAck() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: non-existent-exchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        amqpTemplate.convertAndSend("non-existent-exchange", "TestDirectRouting", map);
        return Result.success(null);
    }
    @GetMapping("/TestMessageAck2")
    @ResponseBody
    public Result TestMessageAck2() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: lonelyDirectExchange test message ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        amqpTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting222", map);
        return Result.success(null);
    }

}
